package com.javaee.ch11.service;


import com.javaee.ch11.common.exceptions.ServiceException;
import com.javaee.ch11.entity.Book;
import com.javaee.ch11.entity.BookCategory;
import com.javaee.ch11.mapper.BookCategoryMapper;
import com.javaee.ch11.web.dto.BookCategoryQueryDto;
import com.javaee.ch11.web.dto.BookQueryDto;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;


@Service
@Transactional
public class BookCategoryService {
    @Autowired
    private BookCategoryMapper bookCategoryMapper;

    public List<BookCategory> listAll() {
        return bookCategoryMapper.selectAll();
    }

    public BookCategory getById(Long id) {
        return bookCategoryMapper.selectById(id);
    }

    public void add(BookCategory bookCategory) {
        if (bookCategoryMapper.insert(bookCategory) != 1) {
            throw new ServiceException("图书分类添加失败");
        }
    }

    public void updateById(BookCategory bookCategory) {
        if (bookCategoryMapper.updateById(bookCategory) != 1) {
            throw new ServiceException("图书分类更新失败");
        }
    }

    public void deleteById(Long id) {
        if (bookCategoryMapper.deleteById(id) != 1) {
            throw new ServiceException("图书分类删除失败");
        }
    }

    public List<BookCategory> queryBooks(BookCategoryQueryDto queryDto) {
        return bookCategoryMapper.queryBookCategories(queryDto);
    }
}